﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.ViewModels;

namespace ContentCreator.Settings
{
    public class ContentCreatorFieldEditorEvents : ContentDefinitionEditorEventsBase
    {
        private readonly IContentDefinitionManager _contentDefinitionManager;

        public ContentCreatorFieldEditorEvents(IContentDefinitionManager contentDefinitionManager)
        {
            _contentDefinitionManager = contentDefinitionManager;
        }

        public override IEnumerable<TemplateViewModel> PartFieldEditor(ContentPartFieldDefinition definition)
        {
            if (definition.FieldDefinition.Name == "ContentCreatorField")
            {
                var model = definition.Settings.GetModel<ContentCreatorFieldSettings>();

                var contentTypes = _contentDefinitionManager.ListTypeDefinitions();

                //Avoid enumerating several times
                var contentTypeDefinitions = contentTypes as IList<ContentTypeDefinition> ?? contentTypes.ToList();

                //Create the list of content types
                model.ContentTypes = contentTypeDefinitions.Select(t => t.Name).OrderBy(t => t);

                //Create the list of content elements
                var partsAndFields = new List<SelectListItem>();
                foreach (var contentType in contentTypeDefinitions)
                {
                    string typeName = contentType.Name;
                    foreach (var part in contentType.Parts)
                    {
                        string partName = part.PartDefinition.Name;
                        //In case we have a part
                        if (typeName != partName)
                        {
                            if (partName == "TitlePart" || partName == "BodyPart" || partName == "TagsPart") {
                                partsAndFields.Add(new SelectListItem {Value = typeName + "/" + partName, Text = partName, Selected = false});
                            }
                        }
                        //In case we have fields
                        else
                        {
                            partsAndFields.AddRange(part.PartDefinition.Fields.Select(field => new SelectListItem { Value = typeName + "/" + partName + "/" + field.Name, Text = field.Name, Selected = false }));
                        }
                    }
                }

                //Select the first one
                partsAndFields.First().Selected = true;

                model.ContentElements = new SelectList(partsAndFields, "Value", "Text");

                yield return DefinitionTemplate(model);
            }
        }

        public override IEnumerable<TemplateViewModel> PartFieldEditorUpdate(ContentPartFieldDefinitionBuilder builder, IUpdateModel updateModel)
        {
            if (builder.FieldType != "ContentCreatorField")
            {
                yield break;
            }

            var model = new ContentCreatorFieldSettings();
            if (updateModel.TryUpdateModel(model, "ContentCreatorFieldSettings", null, null))
            {
                builder.WithSetting("ContentCreatorFieldSettings.Hint", model.Hint);
                builder.WithSetting("ContentCreatorFieldSettings.ContentType", model.ContentType);
                builder.WithSetting("ContentCreatorFieldSettings.ContentElement", model.ContentElement);
                builder.WithSetting("ContentCreatorFieldSettings.Required", model.Required.ToString(CultureInfo.InvariantCulture));
                builder.WithSetting("ContentCreatorFieldSettings.Multiple", model.Multiple.ToString(CultureInfo.InvariantCulture));
            }

            yield return DefinitionTemplate(model);
        }
    }
}